package com.yl.ydk.ui.activity

import android.view.View
import com.yl.ydk.R
import com.yl.ydk.base.BaseActivity
import com.yl.ydk.bean.UserInfoBean
import com.yl.ydk.presenter.contract.HealthtestQuestionActivityContract
import com.yl.ydk.presenter.impl.HealthtestQuestionActivityImpl
import com.yl.ydk.utils.CommontUtils
import com.yl.ydk.utils.PreferUtil
import com.yl.ydk.view.NoScrollViewPager
import com.blankj.utilcode.utils.ToastUtils
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_healthtest_question.*
import kotlinx.android.synthetic.main.roob_bar.*

open class HealthtestQuestionActivity : BaseActivity(), HealthtestQuestionActivityContract.View,
    View.OnClickListener {

    companion object {
        var healthtestQuestionActivity: HealthtestQuestionActivity? = null
    }

    private var option = 0
    private var Optionsize = 0
    private var totalScore = 0
    private var itemId = 0
    private var tmp = 0

    private val mPresenter = HealthtestQuestionActivityImpl()

    override fun getLayoutId(): Int = R.layout.activity_healthtest_question

    override fun attachView() {
        mPresenter.attachView(this)
    }

    override fun detachView() {
        mPresenter.detachView()
    }

    override fun initView() {

        rlBack.setOnClickListener(this)
        btnDown.setOnClickListener(this)
        btnUp.setOnClickListener(this)
        val testId = intent.getIntExtra("testId", 1)
        mPresenter.getHealthtestQuestion(this, CommontUtils.getHealthtestQuestionPrama(testId))
        healthtestQuestionActivity = this

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.rlBack -> {
                finish()
            }
            R.id.btnDown -> {
                ToastUtils.showLongToastSafe("请点击选项")
//                downFragment()
            }
            R.id.btnUp -> {
                upFragment()
            }
        }
    }

    open fun downFragment() {
        if (option == Optionsize) {
            testFinish()
        } else {
            option++
            vpQuestion.currentItem = option
        }
    }

    open fun upFragment() {
        if (option != 0) {
            option--
            cutScore()
            vpQuestion.currentItem = option
        }
    }

    open fun testFinish() {
        val strData = PreferUtil.getInstance().userInfo
        val userInfoBean = Gson().fromJson<UserInfoBean>(strData, UserInfoBean::class.java)
        mPresenter.getTestFinish(
            this, CommontUtils.getTestFinishPrama(
                itemId, totalScore, userInfoBean.data.data.id.toString()
            )
        )
    }

    open fun addScore(score: Int) {
        tmp = score
        totalScore += score
    }

    open fun cutScore(){
        totalScore -= tmp
    }

    override fun getSubjectId(id: Int) {
        this.itemId = id
    }

    override fun getFragmentSize(size: Int) {
        this.Optionsize = size
    }

    override fun getVpQuestion(): NoScrollViewPager = vpQuestion

    override fun getActivity(): HealthtestQuestionActivity = this

    override fun showError(message: String?) {
        ToastUtils.showLongToastSafe(message)
    }

}